package com.bdqn;

import com.bdqn.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import redis.clients.jedis.Jedis;

public class JedisTester {
    private Jedis jedis;

    @Test
    public void setup() {
        //连接redis服务器，127.0.0.1:6379
        jedis = new Jedis("127.0.0.1", 6379);
        //权限认证，指定密码 8.
        jedis.auth("123456");
    }
    @Test
    public void testString() {
        //-----添加数据---------- 4.
        jedis.set("name", "ktjiaoyu");//向key-->name中放入了value-->ktjiaoyu
        System.out.println(jedis.get("name"));//执行结果：ktjiaoyu
        jedis.append("name", "czkt"); //拼接
        System.out.println(jedis.get("name"));
         jedis.del("name"); //删除某个键
        System.out.println(jedis.get("name"));
        //设置多个键值对
        jedis.mset("name", "ktjiaoyu", "age", "17", "add", "cz");
        jedis.incr("age"); //进行加1操作
        System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("add"));
    }

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;


    @Test
    public  void testHash() {
        User user = new User("张三", "123456");
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("user", "usrName", user.getUsrName());
        hashOperations.put("user", "usrPassword", user.getUsrPassword());
        Object user1 = hashOperations.get("user", "usrName");
        System.out.println(user1.toString());
    }

    @Test
    public void testList() {
        ListOperations<String, String> list = redisTemplate.opsForList();
        list.leftPush("list", "accp");
        list.leftPush("list", "bdqn");
        list.leftPush("list", "czkt");
        String value = (String) list.leftPop("list");
        String right = (String) list.rightPop("list");
        System.out.println("list value :" + value.toString());
        System.out.println("right value :" + right.toString());
    }

    }